package org.ehe.erp.controller.batch;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.erp.domain.batch.bo.ErpBatchBo;
import org.ehe.erp.domain.batch.vo.ErpBatchVo;
import org.ehe.erp.service.batch.ErpBatchInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 批次管理
 * @author :zhangnn
 * @className :ErpBatchInfoController
 * @description: TODO
 * @date 2025-08-18 11:00:29
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/erp/batch")
public class ErpBatchInfoController extends BaseController {

    private final ErpBatchInfoService batchInfoService;

    /**
     * 查询批次信息列表
     */
    @GetMapping("/list")
    public TableDataInfo<ErpBatchVo> list(ErpBatchBo bo, PageQuery pageQuery) {
        return batchInfoService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取批次信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("erp:batch:view")
    @GetMapping("/{id}")
    public R<ErpBatchVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long id) {
        return R.ok(batchInfoService.queryById(id));
    }

    /**
     * 新增批次信息
     */
    @SaCheckPermission("erp:batch:add")
    @Log(title = "批次信息新增", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpBatchBo bo) {
        //校验批次名称是否存在
        long i = batchInfoService.selectByName(bo.getTypeName());
        if(i > 0){
            return R.fail("批次类型存在");
        }
        return toAjax(batchInfoService.insertByBo(bo));
    }


    /**
     * 修改批次信息
     */
    @SaCheckPermission("erp:batch:update")
    @Log(title = "批次信息修改", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public R<Void> update(@Validated(AddGroup.class) @RequestBody ErpBatchBo bo) {
        //校验批次名称是否存在
        long i = batchInfoService.selectByName(bo.getTypeName());
        if(i > 0){
            return R.fail("批次类型存在，修改失败！");
        }
        return toAjax(batchInfoService.updateByBo(bo));
    }

    /**
     * 删除批次信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("erp:batch:remove")
    @Log(title = "批次信息删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(batchInfoService.deleteWithValidByIds(List.of(ids), true));
    }

}
